package com.micosco.taskremind;


import android.content.Context;

import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.micosco.taskremind.data.Task;
import com.micosco.taskremind.data.TaskDAO;
import com.micosco.taskremind.data.TaskDAOProxy;
import com.micosco.taskremind.data.TaskDatabase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RunWith(AndroidJUnit4.class)
public class TaskDAOProxyTest {
    private TaskDAO dao;
    private TaskDatabase db;

    @Before
    public void init() {
        Context context = ApplicationProvider.getApplicationContext();
        db = Room.inMemoryDatabaseBuilder(context, TaskDatabase.class).build();
        dao = new TaskDAOProxy(db.taskDAO());
    }

    @After
    public void end() {
        db.close();
    }

    @Test
    public void writeTaskAndRead() {
        Task task = new Task("title", "sub", "none", LocalDate.now(), LocalDateTime.now().plusDays(1), false);
        LocalDateTime now = LocalDateTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        dao.add(task);
        List<Task> temp = dao.findAll();
        LocalDateTime lastModify = temp.get(0).getLastModify();
        Assert.assertTrue(lastModify.isAfter(now));
    }
}
